<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery" style="margin-top: 10px">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">

            <a-form-item label="物资名称" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <a-input placeholder="请输入物资名称" v-model="queryParam.name"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">

            <a-form-item label="物资规格" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <a-input placeholder="请输入物资规格" v-model="queryParam.specifications"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="请选择物资分类">
              <j-search-select-tag type="list" v-model="queryParam.assetType"
                                   :trigger-change="true"  dict="gf_zcgl_asset_type" :disabled="disableSubmit"
                                   placeholder="请选择物资分类"/>
            </a-form-item>

          </a-col>
          <a-col  v-if="queryParam.assetType == 'BGYP01'"   :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="请选择办公子分类">
              <j-search-select-tag    type="list" v-model="queryParam.bgypType"
                                   :trigger-change="true"  dict="bgyp_type" :disabled="disableSubmit"
                                   placeholder="请选择子分类"/>
            </a-form-item>

          </a-col>

          <a-col  v-if="queryParam.assetType == 'CHWZ01'"   :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="请选择存货物资分类">
              <j-search-select-tag    type="list" v-model="queryParam.chwzType"
                                   :trigger-change="true"  dict="chwz_type" :disabled="disableSubmit"
                                   placeholder="请选择子分类"/>
            </a-form-item>

          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button       type="primary" @keyup.enter="searchQuery" icon="search">查询</a-button>
<!--              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>-->
<!--              <a @click="handleToggleSearch" style="margin-left: 8px">-->
<!--                {{ toggleSearchStatus ? '收起' : '展开' }}-->
<!--                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>-->
<!--              </a>-->
            </span>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">

      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
<!--      <a-button type="primary" icon="download" @click="handleExportXls('物资档案')">导出</a-button>-->
<!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">-->
        <a-button type="primary" @click="handleImportExcelData"  icon="import">导入</a-button>
      <!-- 查询区域-END v-has="'assetInfo:temp'"-->
      <a-button  type="primary" icon="download" @click="handleExportXlsTemplate('固定资产导入模板')">
        模板下载
      </a-button>
<!--      </a-upload>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>
    <a-modal title="物资信息导入"     ref="reasonModal" :visible="aavisible" @ok="handleReasonOk" @cancel="handleReasonCancel">
      <a-form :form="form">
        <a-upload :beforeUpload="beforeUpload"  name="file" :showUploadList="false" :multiple="false"
                  :headers="tokenHeader"
                  @change="handleImportExcel"
                  :action="importExcelUrl"
                  >

          <a-button type="primary" icon="import">导入</a-button>
        </a-upload>
      </a-form>
      <template slot="footer">
        <a></a>
      </template>

    </a-modal>
    <!-- table区域-begin -->
    <div>
<!--      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">-->
<!--        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项-->
<!--        <a style="margin-left: 24px" @click="onClearSelected">清空</a>-->
<!--      </div>-->

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
                <a-popconfirm title="确定删除吗?"  style="margin-left: 10px"   @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
<!--          <a-divider type="vertical" />-->
<!--          <a-dropdown>-->
<!--            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>-->
<!--            <a-menu slot="overlay">-->
<!--              <a-menu-item>-->
<!--                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">-->
<!--                  <a>删除</a>-->
<!--                </a-popconfirm>-->
<!--              </a-menu-item>-->
<!--            </a-menu>-->
<!--          </a-dropdown>-->
        </span>

      </a-table>
    </div>

    <zcglSupplies-modal   :addFlag = 'addFlag' ref="modalForm" @ok="modalFormOk"></zcglSupplies-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import ZcglSuppliesModal from './modules/ZcglSuppliesModal'
  import { downFile } from '../../api/manage'
  import JSearchSelectTag from '../../components/dict/JSearchSelectTag'

  export default {
    name: "ZcglSuppliesList",
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      ZcglSuppliesModal,JSearchSelectTag
    },
    data () {
      return {
        isorter: {
          column: '',
          order: ''
        },
        fileList:[],
        aavisible: false,
        addFlag:false,
        description: '物资档案管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },


          {
            title:'物资名称',
            align:"center",
            dataIndex: 'name'
          },
          {
            title:'物资编码',
            align:"center",
            dataIndex: 'code'
          },
          {
            title:'物资分类',
            align:"center",
            dataIndex: 'assetType_dictText'
          },
          {
            title:'子分类',
            align:"center",
             dataIndex: 'bgypType_dictText',
            // customRender:function (t,r,index) {
            //   if(r.assetType_dictText.indexOf("BG")!=-1){
            //     return r.bgypType_dictText;
            //   }
            //   if(r.assetType_dictText == '存货物资'){
            //     return r.chwzType_dictText;
            //   }
            // }
          },

          {
            title:'规格型号',
            align:"center",
            dataIndex: 'specifications'
          },
          {
            title:'单位',
            align:"center",
            dataIndex: 'unit'
          },
          // {
          //   title:'价格',
          //   align:"center",
          //   dataIndex: 'price'
          // },
          {
            title: '图片',
            align: 'center',
            dataIndex: 'image',
            scopedSlots: { customRender: 'imgSlot' }
          },
          {
            title:'安全库存',
            align:"center",
            dataIndex: 'jiexian'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            // fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/zcgl/assetFixed/oldList",
          delete: "/zcgl/assetFixed/delete",
          deleteBatch: "/zcgl/assetFixed/deleteBatch",
          exportXlsUrl: "/zcgl/assetFixed/exportXls",
          importExcelUrl: "/zcgl/assetFixed/importExcel",
        },
        dictOptions:{},
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      beforeUpload(file) {

        return new Promise((resolve, reject) => {
          this.$confirm({
            content: '确定导入吗？',
            onOk: () => {
              console.log('ok ')
              resolve(file);
            },
            onCancel: () => {
              console.log('cancel')
              reject(file);
            },
          })
        });
      },
      /* 导入 */
      handleImportExcel (info) {
        if (info.file.status !== 'uploading') {
          console.log(info.file, info.fileList)
        }
        if (info.file.status === 'done') {
          if (info.file.response.success) {
            // this.$message.success(`${info.file.name} 文件上传成功`);
            if (info.file.response.code === 201) {
              let { message, result: { msg, fileUrl, fileName } } = info.file.response
              let href = window._CONFIG['domianURL'] + fileUrl
              this.$warning({
                title: message,
                content: (
                  <div>
                    <span>{msg}</span>
                    <br/>
                    <span>具体详情请
                    <a href={href} target="_blank" download={fileName}>点击下载</a>
                  </span>
                  </div>
                )
              })

              // `<div>
              //   <span>${msg}</span><br/>
              //   <span>具体详情请 <a href=${href} target="_blank" download=${fileName}>点击下载</a> </span>
              // </div>`
            } else {
              this.$warning({
                title: "导入结果",
                content: (
                  info.file.response.message || `${info.file.name} 文件上传成功`
                ),
                onOk: () => {
                  this.aavisible = false
                },
              })


            }
            this.loadData()
          } else {
            this.$warning({
              title: "导入结果",
              content: (
                `${info.file.name} ${info.file.response.message}.`
              ),
              onOk: () => {
                this.aavisible = false
              },
            })

          }
        } else if (info.file.status === 'error') {
          alert(`文件上传失败: ${info.file.msg} `)
        }
      },
      assetTypeChange(e){
        this.url.importExcelUrl = 'zcgl/assetFixed/importExcel';
      },
      /* 导入 */
      handleImportExcelData (info) {
        this.aavisible = true
      },
      handleReasonOk () {
        this.aavisible = false
      },
      handleReasonCancel () {
        this.aavisible = false
      },
      handleAdd() {
        this.addFlag = true
        this.$refs.modalForm.add()
        this.$refs.modalForm.title = '新增'
        this.$refs.modalForm.disableSubmit = false

      },
      handleEdit(d){
        this.addFlag = false
        this.$refs.modalForm.edit(d)
        this.$refs.modalForm.title = '编辑'
        this.$refs.modalForm.disableSubmit = false
        this.$refs.modalForm.xiugaiFlag = true
      },
      handleExportXlsTemplate (fileName) {
        if (!fileName || typeof fileName != 'string') {
          fileName = '模板'
        }
        console.log('下载模板：' + fileName)
        downFile('zcgl/assetFixed/exportXlsTemplate', { title: fileName }).then((data) => {
          if (!data) {
            this.$message.warning('模板下载失败')
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName + '.xls')
          } else {
            let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName + '.xls')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link) //下载完成移除元素
            window.URL.revokeObjectURL(url) //释放掉blob对象
          }
        })
      },
      initDictConfig(){
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>